<template>
  <el-button type="primary" @click="addCodeDialog = true">新增入群码</el-button>
  <el-dialog v-model="addCodeDialog" title="新增入群码" width="350px">
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="使用者QQ" prop="qq">
        <el-input v-model="form.qq"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addCodeDialog = false">取消</el-button>
        <el-button type="primary" @click="doSubmit(formRef)" :loading="loading">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { inject, reactive, ref } from 'vue'
import { ElForm, ElMessage } from 'element-plus'
import { addCode } from '@/api/admin'

const reload: any = inject('reload')
const form = reactive({
  qq: ''
})
const rules = {
  qq: [{
    required: true,
    message: '请输入使用者QQ',
    trigger: 'change'
  }]
}
const addCodeDialog = ref<boolean>(false)
const loading = ref<boolean>(false)
type FormInstance = InstanceType<typeof ElForm>
const formRef = ref<FormInstance>()
const doSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      loading.value = true
      addCode(form.qq).then(() => {
        ElMessage.success('新增成功')
        addCodeDialog.value = false
        reload()
      }).finally(() => {
        loading.value = false
      })
    } else {
      return false
    }
  })
}
</script>

<style scoped>

</style>
